package org.easydarwin.util;

import android.util.Log;

import org.easydarwin.conf.Config;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ConcurrentLinkedQueue;


/**
 * Created by stamSuper on 2017/11/13.
 * 功能：
 */

public class LogUtil {
    private static Boolean MYLOG_WRITE_TO_FILE = false;// 日志写入文件开关
    private static String TAG = Config.TAG;
    private static SimpleDateFormat format_time = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    private static Date date = new Date();
    public static ConcurrentLinkedQueue<String> tempQueue=new ConcurrentLinkedQueue<String>();
    //	private static String time = format_time.format(date);
    private static String time = String.valueOf(System.currentTimeMillis());


    public static void i(String msg){
        i(TAG,msg);
    }

    public static void i(String tag , String msg){
        if (Config.DEBUG){
            Log.i(tag, msg);

        }
        if(MYLOG_WRITE_TO_FILE){
            savelog(msg);
//			saveLog2Local(msg);
        }
    }

    public static void v(String msg){
        v(TAG, msg);
    }
    public static void v(String tag , String msg){
        if (Config.DEBUG){
            Log.v(tag, msg);
        }
        if(MYLOG_WRITE_TO_FILE){
            savelog(msg);
//			saveLog2Local(msg);
        }
    }

    public static void log2file(String tag , String msg , boolean flag){
//		saveLog2Local(msg , flag);
    }

    public static void d( String msg) {
        d(TAG , msg);
    }

    public static void d(String tag, String msg) {
        if (Config.DEBUG){
            Log.d(tag, msg);

        }
        if(MYLOG_WRITE_TO_FILE){
            savelog(msg);
//			saveLog2Local( msg);
        }
    }

    public static void w( String msg) {
        w(TAG , msg);
    }

    public static void w(String tag , String msg) {
        if (Config.DEBUG){
            Log.w(tag, msg);

        }
        if(MYLOG_WRITE_TO_FILE){
            savelog(msg);
//			saveLog2Local(  msg);
        }
    }

    public static void d(String tag, String msg,
                         Throwable throwable) {
        if (Config.DEBUG){
            Log.d(tag, msg, throwable);

        }
        if(MYLOG_WRITE_TO_FILE){
            savelog(msg);
//			saveLog2Local( msg);
        }
    }
    public static void e( String msg){
        e(TAG , msg);
    }
    public static void e(String tag, String msg) {
        if (Config.DEBUG){
            Log.e(tag, msg);
        }
        if(MYLOG_WRITE_TO_FILE){
            savelog(msg);
//			saveLog2Local(msg);
        }
    }
    public static void e(String tag, String msg , Throwable e) {
        if (Config.DEBUG){
            Log.e(tag, msg , e);
//			saveLog2Local(msg);
        }
        if(MYLOG_WRITE_TO_FILE){
            savelog(msg);
//			saveLog2Local(msg);
        }
    }

    public static void e(Exception e){
        StringWriter sw=new StringWriter();
        PrintWriter pw=new PrintWriter(sw);
        e.printStackTrace(pw);
        e(sw.toString());
    }


    private static void savelog(String msg){
        //tempQueue.add(msg);
        //ExecutorManager.add(new LogRunnable());
    }

    public static void saveLog2Local(String msg , boolean flag){
        if(!MYLOG_WRITE_TO_FILE){
            return;
        }

        String fileName = "log-" + "_" + time + (flag ? "_success" : "_failed")+ ".txt";

    }

    public static void saveLog2Local(String msg){
        if(!MYLOG_WRITE_TO_FILE){
            return;
        }
        String fileName = "log_" + "_" + time + ".txt";

    }

    /**
     * 获取打印信息所在方法名，行号等信息
     * @return
     */
    private static String[] getAutoJumpLogInfos() {
        String[] infos = new String[] { "", "", "" };
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        if (elements.length < 5) {
            Log.e("MyLogger", "Stack is too shallow!!!");
            return infos;
        } else {
            infos[0] = elements[4].getClassName().substring(
                    elements[4].getClassName().lastIndexOf(".") + 1);
            infos[1] = elements[4].getMethodName() + "()";
            infos[2] = " at (" + elements[4].getClassName() + ".java:"
                    + elements[4].getLineNumber() + ")";
            return infos;
        }
    }
}
